package org.ru.mse10.cvis.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
import org.ru.mse10.cvis.entity.cv.custom.provider.WSCustomFieldValueProvider;

/**
 * The Class WSInvoker.
 */
public class WSInvoker {

	/**
	 * Invoke ws data provider.
	 * 
	 * @param provider
	 *            the provider
	 * @return the list
	 */
	public static List<String> invokeWSDataProvider(
			WSCustomFieldValueProvider provider) {
		DynamicClientFactory dcf = DynamicClientFactory.newInstance();
		Client client = dcf.createClient(provider.getWsdlLocation());
		Object[] params = new Object[provider.getParams().size()];
		for (int i = 0; i < provider.getParams().size(); i++) {
			params[i] = provider.getParams().get(i).getParamValue();
		}
		try {
			Object[] result = client.invoke(provider.getOperation(), params);
			Object[] realResult = (Object[]) result[0];
			List<String> list = new ArrayList<String>();
			for (Object object : realResult) {
				list.add(String.valueOf(object));
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Collections.emptyList();
	}
}
